﻿namespace com.stifel.ServerBuildMgmt
{
    using System;

    using com.stifel.Extensions.SharePointExtensions;
    using com.stifel.Extensions.StringExtensions;
    using com.stifel.ServerBuildMgmtDao;
    using com.stifel.SharePoint;
    using com.stifel.SharePoint.Linq.SymbolExtensions;

    using Microsoft.SharePoint;

    /// <summary>
    /// List Item Events
    /// </summary>
    public class VirtualServerBuildRequestEventReceiver : SPItemEventReceiver
    {
        #region Methods

        /// <summary>
        /// The list received a context event.
        /// </summary>
        public override void ContextEvent( SPItemEventProperties properties )
        {
            base.ContextEvent( properties );
        }

        /// <summary>
        /// An item was added.
        /// </summary>
        public override void ItemAdded( SPItemEventProperties properties )
        {
            base.ItemAdded( properties );
        }

        /// <summary>
        /// An item is being added.
        /// </summary>
        public override void ItemAdding( SPItemEventProperties properties )
        {
            base.ItemAdding( properties );
        }

        /// <summary>
        /// An attachment was added to the item.
        /// </summary>
        public override void ItemAttachmentAdded( SPItemEventProperties properties )
        {
            base.ItemAttachmentAdded( properties );
        }

        /// <summary>
        /// An attachment is being added to the item.
        /// </summary>
        public override void ItemAttachmentAdding( SPItemEventProperties properties )
        {
            base.ItemAttachmentAdding( properties );
        }

        /// <summary>
        /// An attachment was removed from the item.
        /// </summary>
        public override void ItemAttachmentDeleted( SPItemEventProperties properties )
        {
            base.ItemAttachmentDeleted( properties );
        }

        /// <summary>
        /// An attachment is being removed from the item.
        /// </summary>
        public override void ItemAttachmentDeleting( SPItemEventProperties properties )
        {
            base.ItemAttachmentDeleting( properties );
        }

        /// <summary>
        /// An item was checked in.
        /// </summary>
        public override void ItemCheckedIn( SPItemEventProperties properties )
        {
            base.ItemCheckedIn( properties );
        }

        /// <summary>
        /// An item was checked out.
        /// </summary>
        public override void ItemCheckedOut( SPItemEventProperties properties )
        {
            base.ItemCheckedOut( properties );
        }

        /// <summary>
        /// An item is being checked in.
        /// </summary>
        public override void ItemCheckingIn( SPItemEventProperties properties )
        {
            base.ItemCheckingIn( properties );
        }

        /// <summary>
        /// An item is being checked out.
        /// </summary>
        public override void ItemCheckingOut( SPItemEventProperties properties )
        {
            base.ItemCheckingOut( properties );
        }

        /// <summary>
        /// An item was deleted.
        /// </summary>
        public override void ItemDeleted( SPItemEventProperties properties )
        {
            base.ItemDeleted( properties );
        }

        /// <summary>
        /// An item is being deleted.
        /// </summary>
        public override void ItemDeleting( SPItemEventProperties properties )
        {
            base.ItemDeleting( properties );
        }

        /// <summary>
        /// A file was converted.
        /// </summary>
        public override void ItemFileConverted( SPItemEventProperties properties )
        {
            base.ItemFileConverted( properties );
        }

        /// <summary>
        /// A file was moved.
        /// </summary>
        public override void ItemFileMoved( SPItemEventProperties properties )
        {
            base.ItemFileMoved( properties );
        }

        /// <summary>
        /// A file is being moved.
        /// </summary>
        public override void ItemFileMoving( SPItemEventProperties properties )
        {
            base.ItemFileMoving( properties );
        }

        /// <summary>
        /// An item was unchecked out.
        /// </summary>
        public override void ItemUncheckedOut( SPItemEventProperties properties )
        {
            base.ItemUncheckedOut( properties );
        }

        /// <summary>
        /// An item is being unchecked out.
        /// </summary>
        public override void ItemUncheckingOut( SPItemEventProperties properties )
        {
            base.ItemUncheckingOut( properties );
        }

        /// <summary>
        /// An item was updated.
        /// </summary>
        public override void ItemUpdated( SPItemEventProperties properties )
        {
            base.ItemUpdated( properties );
            if ( properties.ListItem.ContentType.Name.Equals( StifelVirtualServerRequest.GetContentTypeName<StifelVirtualServerRequest>( ) ) )
            {
                if ( isControl( properties.ListItem ) )
                {
                    EventFiringEnabled = false;
                    StifelServerBuildDao dao = new StifelServerBuildDao( properties.WebUrl );
                    StifelVirtualServerRequest o = dao.Get( properties.ListItem.UniqueId );
                    o.Control = null;
                    dao.Update( o );
                    SPUserInfoExpanded user = properties.Web.GetUserByLoginName( properties.UserLoginName );
                    RequestStatusWorkflow.Start( o, properties.Web, user );
                    EventFiringEnabled = true;
                }
            }
        }

        /// <summary>
        /// An item is being updated.
        /// </summary>
        public override void ItemUpdating( SPItemEventProperties properties )
        {
            base.ItemUpdating( properties );
        }

        StifelVirtualServerRequest getStifelVirtualServerRequest( SPItemEventDataCollection value )
        {
            StifelVirtualServerRequest r = new StifelVirtualServerRequest( );
            r.ActivityAuditLog = value[ r.GetSharePointLinqColumnAttributeName( a => a.ActivityAuditLog ) ].ToString( );
            r.ADConfigNotificationDate = value[ r.GetSharePointLinqColumnAttributeName( a => a.ADConfigNotificationDate ) ].ToString( ).ToDateTime( );
            r.ADConfigStatus = ( ADConfigStatus ) Enum.Parse( typeof( ADConfigStatus ), value[ r.GetSharePointLinqColumnAttributeName( a => a.ADConfigStatus ) ].ToString( ) );
            r.ApprovalNotificationDate = value[ r.GetSharePointLinqColumnAttributeName( a => a.ApprovalNotificationDate ) ].ToString( ).ToDateTime( );
            r.ApprovalStatus = ( ApprovalStatus ) Enum.Parse( typeof( ApprovalStatus ), value[ r.GetSharePointLinqColumnAttributeName( a => a.ApprovalStatus ) ].ToString( ) );
            r.AVConfigNotificationDate = value[ r.GetSharePointLinqColumnAttributeName( a => a.AVConfigNotificationDate ) ].ToString( ).ToDateTime( );
            r.AVConfigStatus = ( AVConfigStatus ) Enum.Parse( typeof( AVConfigStatus ), value[ r.GetSharePointLinqColumnAttributeName( a => a.AVConfigStatus ) ].ToString( ) );
            r.BackupConfigNotificationDate = value[ r.GetSharePointLinqColumnAttributeName( a => a.BackupConfigNotificationDate ) ].ToString( ).ToDateTime( );
            r.BackupConfigStatus = ( BackupConfigStatus ) Enum.Parse( typeof( BackupConfigStatus ), value[ r.GetSharePointLinqColumnAttributeName( a => a.BackupConfigStatus ) ].ToString( ) );
            r.BuildNotificationDate = value[ r.GetSharePointLinqColumnAttributeName( a => a.BuildNotificationDate ) ].ToString( ).ToDateTime( );
            r.BuildStatus = ( BuildStatus ) Enum.Parse( typeof( BuildStatus ), value[ r.GetSharePointLinqColumnAttributeName( a => a.BuildStatus ) ].ToString( ) );
            r.MonitorConfigNotificationDate = value[ r.GetSharePointLinqColumnAttributeName( a => a.MonitorConfigNotificationDate ) ].ToString( ).ToDateTime( );
            r.MonitorConfigStatus = ( MonitorConfigStatus ) Enum.Parse( typeof( MonitorConfigStatus ), value[ r.GetSharePointLinqColumnAttributeName( a => a.MonitorConfigStatus ) ].ToString( ) );
            r.NotificationId = value[ r.GetSharePointLinqColumnAttributeName( a => a.NotificationId ) ].ToString( );
            r.OverallStatus = ( OverallStatus ) Enum.Parse( typeof( OverallStatus ), value[ r.GetSharePointLinqColumnAttributeName( a => a.OverallStatus ) ].ToString( ) );
            r.PatchConfigNotificationDate = value[ r.GetSharePointLinqColumnAttributeName( a => a.PatchConfigNotificationDate ) ].ToString( ).ToDateTime( );
            r.PatchConfigStatus = ( PatchConfigStatus ) Enum.Parse( typeof( PatchConfigStatus ), value[ r.GetSharePointLinqColumnAttributeName( a => a.PatchConfigStatus ) ].ToString( ) );
            r.RequestApproved = value[ r.GetSharePointLinqColumnAttributeName( a => a.RequestApproved ) ].ToString( ).ToBoolean( );
            r.RequestCanceled = value[ r.GetSharePointLinqColumnAttributeName( a => a.RequestCanceled ) ].ToString( ).ToBoolean( );
            r.RequestCompleted = value[ r.GetSharePointLinqColumnAttributeName( a => a.RequestCompleted ) ].ToString( ).ToBoolean( );
            r.RequestorNotificationDate = value[ r.GetSharePointLinqColumnAttributeName( a => a.RequestorNotificationDate ) ].ToString( ).ToDateTime( );
            r.SerializedVirtualServerRequest = value[ r.GetSharePointLinqColumnAttributeName( a => a.SerializedVirtualServerRequest ) ].ToString( );
            r.RequestRejected = value[ r.GetSharePointLinqColumnAttributeName( a => a.RequestRejected ) ].ToString( ).ToBoolean( );
            r.RequestorStatus = ( RequestorStatus ) Enum.Parse( typeof( RequestorStatus ), value[ r.GetSharePointLinqColumnAttributeName( a => a.RequestorStatus ) ].ToString( ) );
            r.Title = value[ r.GetSharePointLinqColumnAttributeName( a => a.Title ) ].ToString( );
            return r;
        }

        bool isControl( SPListItem item )
        {
            bool? ok = ( bool? ) item[ "sn_srvbld_Control" ];
            return ok.HasValue ? ok.Value : false;
        }

        #endregion Methods
    }
}